﻿// Copyright (C) 2012 Jeff Tanner <jeff00seattle@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;

namespace WindowsAzure_StorageREST
{
    public class BlobServiceResponse : StorageResponse
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BlobServiceResponse"/> class.
        /// </summary>
        /// <param name="response">The response.</param>
        public BlobServiceResponse(StorageResponse response)
        {
            base.ResponseMessage = response.ResponseMessage;
            base.ResponseHeaders = response.ResponseHeaders;
            base.ResponseCode = response.ResponseCode;
            base.ResponseBody = response.ResponseBody;
        }

        /// <summary>
        /// Gets a blob containers.
        /// </summary>
        /// <returns></returns>
        public List<BlobContainer> RetrieveBlobContainers()
        {
            var blobContainers
                = from blobContainer
                    in XDocument.Parse(ResponseBody).Root.Descendants("Container")
                  select new BlobContainer(null)
                  {
                      Name = blobContainer.Element("Name").Value,
                      Url = new Uri(blobContainer.Element("Url").Value)
                  };

            return blobContainers.ToList();
        }

        /// <summary>
        /// Gets the container blobs.
        /// </summary>
        /// <returns></returns>
        public List<Blob> RetrieveContainerBlobs(StorageEndpoint blobContainerEndpoint, string blobContainerName)
        {
            var blobs
                = from blob
                    in XDocument.Parse(ResponseBody).Root.Descendants("Blob")
                  select new Blob(
                          blobContainerEndpoint,
                          blobContainerName
                          )
                  {
                      Name = blob.Element("Name").Value,
                      Url = new Uri(blob.Element("Url").Value)
                  };

            return blobs.ToList();
        }
    }
}